﻿using DeepZoomPivotConstructor.PivotLib;
using DeepZoomPivotConstructor.PivotLib.PivotSchema;
using DeepZoomPivotConstructor.VisualLib;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace t_PivotLib
{
    [TestClass]
    public class t_PivotProcessor
    {
        [TestMethod]
        public void TestCtor()
        {
            var p = new PivotProcessor(new t_TestingLibrary.TestLogger());
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestSingleImageCollection()
        {
            PivotCollection c = new PivotCollection();
            PivotImage img = new PivotImage(new VisualSingleImage(new FileInfo("pic1.png"), 100));
            img.Name = "Name1";
            img.Description = "Descrip";
            c.Add(img);

            DirectoryInfo inf = new DirectoryInfo(".\\TestSingleImageCollectoin");
            if (inf.Exists)
            {
                inf.Delete(true);
            }

            PivotProcessor p = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var collecitonFile = await p.CreatePivotCollection(c, inf, "TestSingleImageCollectoin");
            Assert.IsTrue(collecitonFile.Exists, "The collection file doesn't exist!");
            Assert.AreEqual(".cxml", collecitonFile.Extension, "Wrong extension for the main guy coming back!");

            var col = LoadCollectoin(collecitonFile);
            Assert.AreEqual(1, col.Items.Length, "Expected only one 'main' item");
            Assert.AreEqual(1, col.Items[0].Item.Length, "Expected only one image!");
            Assert.AreEqual("Name1", col.Items[0].Item[0].Name, "Expected the name to be correctly set!");
            Assert.AreEqual("Descrip", col.Items[0].Item[0].Description, "Descirption for the img didn't make it through");
        }

        [TestMethod]
        public async Task TestEmptyCollection()
        {
            PivotCollection pc = new PivotCollection();
            DirectoryInfo dir = new DirectoryInfo(".\\TestEmptyCollection");
            PivotProcessor proc = new PivotProcessor(new t_TestingLibrary.TestLogger());
            await proc.CreatePivotCollection(pc, dir, "test");
        }

        /// <summary>
        /// Gets back the XML from the file.
        /// </summary>
        /// <param name="collecitonFile"></param>
        /// <returns></returns>
        private Collection LoadCollectoin(FileInfo collecitonFile)
        {
            using (var reader = collecitonFile.OpenText())
            {
                var deser = new XmlSerializer(typeof(Collection));
                return deser.Deserialize(reader) as Collection;
            }
        }

        [TestMethod]
        public async Task TestMultipleImageCollection()
        {
            PivotCollection c = new PivotCollection();
            AddImages(c, 10);

            DirectoryInfo inf = new DirectoryInfo(".\\TestMultipleImageCollection");
            if (inf.Exists)
            {
                inf.Delete(true);
            }

            PivotProcessor p = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var collecitonFile = await p.CreatePivotCollection(c, inf, "TestSingleImageCollectoin");
            Assert.IsTrue(collecitonFile.Exists, "The collection file doesn't exist!");
            Assert.AreEqual(".cxml", collecitonFile.Extension, "Wrong extension for the main guy coming back!");
        }

        [TestMethod]
        public async Task TestCollectionPartitionSingleReturn()
        {
            PivotCollection c = new PivotCollection();
            AddImages(c, 10);

            DirectoryInfo inf = new DirectoryInfo(".\\TestCollectionPartitionSingleReturn");
            if (inf.Exists)
            {
                inf.Delete(true);
            }

            PivotProcessor p = new PivotProcessor(new t_TestingLibrary.TestLogger());
            int count = 0;
            Func<PivotImage, string[]> catIt = pi =>
            {
                count += 1;
                if (count % 2 == 0)
                {
                    return new string[] { "Odd" };
                }
                else
                {
                    return new string[] { "Even" };
                }
            };
            var collecitonFile = await p.CreatePivotCollection(c, inf, "TestSingleImageCollectoin", catIt);
            Assert.IsTrue(collecitonFile.Exists, "The collection file doesn't exist!");
            Assert.AreEqual(".cxml", collecitonFile.Extension, "Wrong extension for the main guy coming back!");
            Assert.IsTrue(File.Exists(collecitonFile.DirectoryName + "\\Odd.cxml"), "Could not find the odd collection!");
            Assert.IsTrue(File.Exists(collecitonFile.DirectoryName + "\\Even.cxml"), "Could nto find the even collection!");

            var cls = p.ClassesUsed;
            Assert.AreEqual(2, cls.Length, "Expected 2 classes to have been seen");
            Assert.IsTrue(cls.Contains("Even"), "Expected even to be in there");
            Assert.IsTrue(cls.Contains("Odd"), "Expected odd to be in there");
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestCollectionPartitionMultipleReturn()
        {
            PivotCollection c = new PivotCollection();
            AddImages(c, 10);

            DirectoryInfo inf = new DirectoryInfo(".\\TestCollectionPartitionMultipleReturn");
            if (inf.Exists)
            {
                inf.Delete(true);
            }

            PivotProcessor p = new PivotProcessor(new t_TestingLibrary.TestLogger());
            Func<PivotImage, string[]> catIt = pi =>
            {
                return new string[] { "1", "2", "3" };
            };
            var collecitonFile = await p.CreatePivotCollection(c, inf, "TestSingleImageCollectoin", catIt);
            Assert.IsTrue(collecitonFile.Exists, "The collection file doesn't exist!");
            Assert.AreEqual(".cxml", collecitonFile.Extension, "Wrong extension for the main guy coming back!");
            Assert.IsTrue(File.Exists(collecitonFile.DirectoryName + "\\1.cxml"), "Could not find the odd collection!");
            Assert.IsTrue(File.Exists(collecitonFile.DirectoryName + "\\2.cxml"), "Could nto find the even collection!");
            Assert.IsTrue(File.Exists(collecitonFile.DirectoryName + "\\3.cxml"), "Could nto find the even collection!");
            Assert.AreEqual(3, p.ClassesUsed.Length, "Incorrect number of classes");
        }

        /// <summary>
        /// Add some number of images to the thing
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="numberOfImages"></param>
        private void AddImages(PivotCollection collection, int numberOfImages)
        {
            var sourceImage = new FileInfo("pic1.png");
            Assert.IsTrue(sourceImage.Exists, "pic1 was not deployed");

            for (int i = 0; i < numberOfImages; i++)
            {
                var outImage = new FileInfo(string.Format(@".\{0}.png", i));
                sourceImage.CopyTo(outImage.FullName, true);
                var si = new VisualSingleImage(outImage, 100);
                si.SetAsComposableUnit(null);
                PivotImage img = new PivotImage(si);
                collection.Add(img);
            }
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetString()
        {
            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            PivotImage target = new PivotImage(v) { { "name", "dude" } };
            PivotCollection c = new PivotCollection() { target };
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var fac = GetFacet(await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetString"), "Name"), 0, 0);
            Assert.AreEqual("name", fac.Name, "Facet name didn't come back right");
            Assert.AreEqual("dude", fac.String[0].Value, "Got incorrect type for the value");
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetNumber()
        {
            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            PivotImage target = new PivotImage(v) { { "name", (decimal)44.4 } };
            PivotCollection c = new PivotCollection() { target };
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var fac = GetFacet(await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetString"), "Name"), 0, 0);
            Assert.AreEqual("name", fac.Name, "Facet name didn't come back right");
            Assert.AreEqual((decimal)44.4, fac.Number[0].Value, "Got incorrect type for the value");
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetVerySmallNumber()
        {
            // The SL5 PV control seems to have difficulty displaying filter lists if a
            // number pivot has a very small number different from an integer that isn't quite
            // an integer. Say 5.00001. So, kill those off. :(

            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            PivotImage target = new PivotImage(v) { { "name", (decimal)50.00004 } };
            PivotCollection c = new PivotCollection() { target };
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var fac = GetFacet(await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetString"), "Name"), 0, 0);
            Assert.AreEqual((decimal)50.0, fac.Number[0].Value, "Got incorrect type for the value");
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetVeryNotQuiteSmallNumber()
        {
            // The SL5 PV control seems to have difficulty displaying filter lists if a
            // number pivot has a very small number different from an integer that isn't quite
            // an integer. Say 5.00001. So, kill those off. :(

            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            PivotImage target = new PivotImage(v) { { "name", (decimal)50.00005 } };
            PivotCollection c = new PivotCollection() { target };
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var fac = GetFacet(await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetString"), "Name"), 0, 0);
            Assert.AreEqual((decimal)50.00005, fac.Number[0].Value, "Got incorrect type for the value");
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetVeryNotQuiteSmallNumberSeenInField()
        {
            // The SL5 PV control seems to have difficulty displaying filter lists if a
            // number pivot has a very small number different from an integer that isn't quite
            // an integer. Say 5.00001. So, kill those off. :(

            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            PivotImage target = new PivotImage(v) { { "name", (decimal)45.0000000000001 } };
            PivotCollection c = new PivotCollection() { target };
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var fac = GetFacet(await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetString"), "Name"), 0, 0);
            Assert.AreEqual((decimal)45.0, fac.Number[0].Value, "Got incorrect type for the value");
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetVeryNotQuiteSmallNumberSeenInField2()
        {
            // The SL5 PV control seems to have difficulty displaying filter lists if a
            // number pivot has a very small number different from an integer that isn't quite
            // an integer. Say 5.00001. So, kill those off. :(

            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            PivotImage target = new PivotImage(v) { { "name", (decimal)-15.0000000000001 } };
            PivotCollection c = new PivotCollection() { target };
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var fac = GetFacet(await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetString"), "Name"), 0, 0);
            Assert.AreEqual((decimal)-15.0, fac.Number[0].Value, "Got incorrect type for the value");
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetVeryNotQuiteSmallLessThanNumber()
        {
            // The SL5 PV control seems to have difficulty displaying filter lists if a
            // number pivot has a very small number different from an integer that isn't quite
            // an integer. Say 5.00001. So, kill those off. :(

            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            PivotImage target = new PivotImage(v) { { "name", (decimal)50.99996 } };
            PivotCollection c = new PivotCollection() { target };
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var fac = GetFacet(await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetString"), "Name"), 0, 0);
            Assert.AreEqual((decimal)51.0, fac.Number[0].Value, "Got incorrect type for the value");
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetDateTime()
        {
            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            var now = DateTime.Now;
            PivotImage target = new PivotImage(v) { { "name", now } };
            PivotCollection c = new PivotCollection() { target };
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var fac = GetFacet(await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetString"), "Name"), 0, 0);
            Assert.AreEqual("name", fac.Name, "Facet name didn't come back right");
            Assert.AreEqual(now, fac.DateTime[0].Value, "Got incorrect type for the date");
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetLink()
        {
            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            PivotImage target = new PivotImage(v) { { "name", "dude", new Uri("http://www.cnn.com") } };
            PivotCollection c = new PivotCollection() { target };
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var fac = GetFacet(await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetString"), "Name"), 0, 0);
            Assert.AreEqual("name", fac.Name, "Facet name didn't come back right");
            Assert.AreEqual("dude", fac.Link[0].Name, "Got incorrect type for the href name");
            Assert.AreEqual("http://www.cnn.com", fac.Link[0].Href, "Got incorrect type for the href");
        }

        [ExpectedException(typeof(InvalidOperationException))]
        [DeploymentItem("pic1.png")]
        public async Task TestDifferentFacetTypes()
        {
            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            PivotImage target = new PivotImage(v) { { "name", "dude" }, { "name", "fork", new Uri("http://www.cnn.com") } };
            PivotCollection c = new PivotCollection() { target };
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetString"), "Name");
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetCatalog()
        {
            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            PivotImage target = new PivotImage(v) { { "name", "dude" }, { "name", "fork" } };
            PivotCollection c = new PivotCollection() { target };
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var col = LoadCollectoin(await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetCatalog"), "Name"));
            Assert.AreEqual(1, col.FacetCategories.FacetCategory.Length, "The # of facet categories isn't right");
            Assert.AreEqual("name", col.FacetCategories.FacetCategory[0].Name, "The name of the first facet catagory is not right");
            Assert.AreEqual(true, col.FacetCategories.FacetCategory[0].IsWordWheelVisibleSpecified, "wheel visible specified");
            Assert.AreEqual(true, col.FacetCategories.FacetCategory[0].IsWordWheelVisible, "wheel visible");
            Assert.AreEqual(true, col.FacetCategories.FacetCategory[0].IsMetaDataVisibleSpecified, "metadata visible specified");
            Assert.AreEqual(true, col.FacetCategories.FacetCategory[0].IsMetaDataVisible, "metadata visible");
            Assert.AreEqual(true, col.FacetCategories.FacetCategory[0].IsFilterVisibleSpecified, "filter visible specified");
            Assert.AreEqual(true, col.FacetCategories.FacetCategory[0].IsFilterVisible, "filter visible");
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetCatalogCaseSensitivity()
        {
            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            PivotImage target = new PivotImage(v) { { "name", "dude" }, { "Name", "fork" } };
            PivotCollection c = new PivotCollection() { target };
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var col = LoadCollectoin(await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetCatalogCaseSensitivity"), "Name"));
            Assert.AreEqual(1, col.Items[0].Item[0].Facets.Facet.Length, "# of facets");
            Assert.AreEqual(2, col.Items[0].Item[0].Facets.Facet[0].String.Length, "expected both values in here");
            Assert.AreEqual(1, col.FacetCategories.FacetCategory.Length, "The # of facet categories isn't right");
            Assert.AreEqual("name", col.FacetCategories.FacetCategory[0].Name, "The name of the first facet catagory is not right");
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetCatalogCaseSensitivityTS()
        {
            // Trying to repro an error we see in real life.
            var originalImage = new FileInfo("pic1.png");
            originalImage.CopyTo("i1.png", true);
            originalImage.CopyTo("i2.png", true);
            Visual v1 = new VisualSingleImage(new FileInfo("i1.png"), 100);
            Visual v2 = new VisualSingleImage(new FileInfo("i2.png"), 100);
            v1.SetAsComposableUnit(null);
            v2.SetAsComposableUnit(null);

            PivotImage target1 = new PivotImage(v1) { { "TS", (decimal)5.0 }, { "ts", (decimal)1.0 } };
            PivotImage target2 = new PivotImage(v2) { { "ts", (decimal)5.0 }, { "TS", (decimal)1.0 } };
            PivotCollection c = new PivotCollection() { target1, target2 };
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var col = LoadCollectoin(await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetCatalogCaseSensitivity"), "Name"));
            Assert.AreEqual(1, col.Items[0].Item[0].Facets.Facet.Length, "# of facets");
            Assert.AreEqual(2, col.Items[0].Item[0].Facets.Facet[0].Number.Length, "expected both values in here");
            Assert.AreEqual(1, col.FacetCategories.FacetCategory.Length, "The # of facet categories isn't right");
            Assert.AreEqual("TS", col.FacetCategories.FacetCategory[0].Name, "The name of the first facet catagory is not right");
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetCatalogCaseSensitivity3CatIterations()
        {
            // Trying to repro an error we see in real life.
            var originalImage = new FileInfo("pic1.png");
            originalImage.CopyTo("i1.png", true);
            originalImage.CopyTo("i2.png", true);
            originalImage.CopyTo("i3.png", true);

            Visual v1 = new VisualSingleImage(new FileInfo("i1.png"), 100);
            Visual v2 = new VisualSingleImage(new FileInfo("i2.png"), 100);
            Visual v3 = new VisualSingleImage(new FileInfo("i3.png"), 100);
            v1.SetAsComposableUnit(null);
            v2.SetAsComposableUnit(null);
            v3.SetAsComposableUnit(null);
            PivotImage target1 = new PivotImage(v1) { { "TmeS", (decimal)5.0 }, { "tMEs", (decimal)1.0 }, { "tMES", (decimal)5.0 } };
            PivotImage target2 = new PivotImage(v2) { { "tMEs", (decimal)1.0 }, { "TmeS", (decimal)5.0 }, { "tMES", (decimal)5.0 } };
            PivotImage target3 = new PivotImage(v3) { { "tMES", (decimal)5.0 }, { "TmeS", (decimal)5.0 }, { "tMEs", (decimal)1.0 } };
            PivotCollection c = new PivotCollection() { target1, target2, target3 };
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var col = LoadCollectoin(await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetCatalogCaseSensitivity"), "Name"));
            Assert.AreEqual(1, col.Items[0].Item[0].Facets.Facet.Length, "# of facets");
            Assert.AreEqual(3, col.Items[0].Item[0].Facets.Facet[0].Number.Length, "expected both values in here");
            Assert.AreEqual(1, col.FacetCategories.FacetCategory.Length, "The # of facet categories isn't right");
            Assert.AreEqual("TmeS", col.FacetCategories.FacetCategory[0].Name, "The name of the first facet catagory is not right");
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestCatalogUTF()
        {
            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            PivotImage target = new PivotImage(v) { { "name", "dude" }, { "name", "fork" } };
            PivotCollection c = new PivotCollection() { target };
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var r = await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetCatalog"), "Name");
            using (var rdr = r.OpenText())
            {
                var s = rdr.ReadToEnd();
                Assert.IsTrue(s.ToUpper().Contains("UTF-8"));
            }
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetCatalogEmpty()
        {
            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            PivotImage target = new PivotImage(v);
            PivotCollection c = new PivotCollection() { target };
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var r = await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetCatalogEmpty"), "Name");
            using (var rdr = r.OpenText())
            {
                var s = rdr.ReadToEnd();
                Assert.IsFalse(s.Contains("<Facets />"));
            }
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetCategoriesEmpty()
        {
            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            PivotImage target = new PivotImage(v);
            PivotCollection c = new PivotCollection() { target };
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var r = await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetCatalogEmpty"), "Name");
            using (var rdr = r.OpenText())
            {
                var s = rdr.ReadToEnd();
                Assert.IsFalse(s.Contains("<FacetCategories />"));
            }
        }
        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetCatalogTypeString()
        {
            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            PivotImage target = new PivotImage(v) { { "name", "dude" } };
            PivotCollection c = new PivotCollection() { target };
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var col = LoadCollectoin(await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetCatalogTypeString"), "Name"));
            Assert.AreEqual(facetType.String, col.FacetCategories.FacetCategory[0].Type, "String type isn't right");
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetCatalogTypeNumber()
        {
            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            PivotImage target = new PivotImage(v) { { "name", (decimal)5.3 } };
            PivotCollection c = new PivotCollection() { target };
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var col = LoadCollectoin(await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetCatalogTypeNumber"), "Name"));
            Assert.AreEqual(facetType.Number, col.FacetCategories.FacetCategory[0].Type, "number type isn't right");
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetCatalogTypeDateTime()
        {
            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            PivotImage target = new PivotImage(v) { { "name", DateTime.Now } };
            PivotCollection c = new PivotCollection() { target };
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var col = LoadCollectoin(await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetCatalogTypeDateTime"), "Name"));
            Assert.AreEqual(facetType.DateTime, col.FacetCategories.FacetCategory[0].Type, "DateTime type isn't right");
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetCatalogTypeDateLink()
        {
            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            PivotImage target = new PivotImage(v) { { "name", "Fork", new Uri("http://www.cnn.com") } };
            PivotCollection c = new PivotCollection() { target };
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var col = LoadCollectoin(await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetCatalogTypeDateLink"), "Name"));
            Assert.AreEqual(facetType.Link, col.FacetCategories.FacetCategory[0].Type, "DateTime type isn't right");
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetCatalogSetWorldWheel()
        {
            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            PivotImage target = new PivotImage(v) { { "name", "dude" } };
            PivotCollection c = new PivotCollection() { target };
            c.FacetCatagories["name"].IsWordWheelVisible = false;
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var col = LoadCollectoin(await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetCatalogSetWorldWheel"), "Name"));

            Assert.AreEqual(false, col.FacetCategories.FacetCategory[0].IsWordWheelVisible, "wheel visible");
            Assert.AreEqual(true, col.FacetCategories.FacetCategory[0].IsMetaDataVisible, "metadata visible");
            Assert.AreEqual(true, col.FacetCategories.FacetCategory[0].IsFilterVisible, "filter visible");
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetCatalogSetMetadata()
        {
            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            PivotImage target = new PivotImage(v) { { "name", "dude" } };
            PivotCollection c = new PivotCollection() { target };
            c.FacetCatagories["name"].IsMetaDataVisible = false;
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var col = LoadCollectoin(await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetCatalogSetMetadata"), "Name"));

            Assert.AreEqual(true, col.FacetCategories.FacetCategory[0].IsWordWheelVisible, "wheel visible");
            Assert.AreEqual(false, col.FacetCategories.FacetCategory[0].IsMetaDataVisible, "metadata visible");
            Assert.AreEqual(true, col.FacetCategories.FacetCategory[0].IsFilterVisible, "filter visible");
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetCatalogSetFilter()
        {
            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            PivotImage target = new PivotImage(v) { { "name", "dude" } };
            PivotCollection c = new PivotCollection() { target };
            c.FacetCatagories["name"].IsFilterVisible = false;
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var col = LoadCollectoin(await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetCatalogSetFilter"), "Name"));

            Assert.AreEqual(true, col.FacetCategories.FacetCategory[0].IsWordWheelVisible, "wheel visible");
            Assert.AreEqual(true, col.FacetCategories.FacetCategory[0].IsMetaDataVisible, "metadata visible");
            Assert.AreEqual(false, col.FacetCategories.FacetCategory[0].IsFilterVisible, "filter visible");
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetCatalogSetNumberOnNonNumber()
        {
            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            PivotImage target = new PivotImage(v) { { "name", "dude" } };
            PivotCollection c = new PivotCollection() { target };
            c.FacetCatagories["name"].NumberFormat = "F2";
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var col = LoadCollectoin(await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetCatalogSetFilter"), "Name"));

            Assert.IsNull(col.FacetCategories.FacetCategory[0].Format, "format");
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetCatalogSetNumberOnBadNumber()
        {
            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            PivotImage target = new PivotImage(v) { { "name", (decimal)20934260485.0967 } };
            PivotCollection c = new PivotCollection() { target };
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var col = LoadCollectoin(await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetCatalogSetFilter"), "Name"));

            Assert.IsNull(col.FacetCategories.FacetCategory[0].Format, "format");
        }

        [TestMethod]
        [DeploymentItem("pic1.png")]
        public async Task TestFacetCatalogSetNumberOnNumber()
        {
            Visual v = new VisualSingleImage(new FileInfo("pic1.png"), 100);
            PivotImage target = new PivotImage(v) { { "name", (decimal)5.5 } };
            PivotCollection c = new PivotCollection() { target };
            c.FacetCatagories["name"].NumberFormat = "F2";
            var pp = new PivotProcessor(new t_TestingLibrary.TestLogger());
            var col = LoadCollectoin(await pp.CreatePivotCollection(c, new DirectoryInfo(".\\TestFacetCatalogSetFilter"), "Name"));

            Assert.AreEqual("F2", col.FacetCategories.FacetCategory[0].Format, "format");
        }

        /// <summary>
        /// Gets the requested facet. Assumes everything exists!
        /// </summary>
        /// <param name="fileInfo"></param>
        /// <param name="imageIndex"></param>
        /// <param name="facetIndex"></param>
        /// <returns></returns>
        private DeepZoomPivotConstructor.PivotLib.PivotSchema.facet GetFacet(FileInfo fileInfo, int imageIndex, int facetIndex)
        {
            Collection c = LoadCollectoin(fileInfo);
            return c.Items[0].Item[imageIndex].Facets.Facet[facetIndex];
        }

    }
}
